package presenter
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import model.Paciente;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.rpc.events.ResultEvent;
	
	import remote.PacienteRO;
	
	import view.PacienteUI;

	public class PacientePresenter	{	
		
		[Bindable]
		private var _pacienteRO: PacienteRO;
		private  var _paciente: Paciente;
		private var _view: PacienteUI;
		private var retorno:Boolean;
		
		
		public function PacientePresenter()
		{
			this._view = new PacienteUI();
			this._view.title = "Inclusão de Pacientes";
			this._view.addEventListener(CloseEvent.CLOSE, onClose);
			
			this._view.btnSalvar.addEventListener(MouseEvent.CLICK, salvar);
			this._view.btnCancelar.addEventListener(MouseEvent.CLICK, cancelar);
			
			this._view.pacienteForm.addEventListener(KeyboardEvent.KEY_DOWN, enterPress);

			
		}
			
		private function onClose(evt:CloseEvent):void {
			/*		if (evt.type == "cancel") {
				BindingUtils.bindProperty(paciente, "id", aluno2, "id");
				BindingUtils.bindProperty(aluno, "nome", aluno2, "nome");
				BindingUtils.bindProperty(aluno, "email", aluno2, "email");
				BindingUtils.bindProperty(aluno, "celular", aluno2, "celular");		
			}*/
			this._view.close(null);			
		}
		
		
		
		public function getView():PacienteUI {
			return _view;
		}
		
		public function cancelar(evt: MouseEvent): void {
			onClose(new CloseEvent("cancel"));
		}

		public function salvar(evt: MouseEvent): void {
			//if (Validator.validateAll([this.view.emailValidator]).length == 0)  {
				this._pacienteRO = PacienteRO.getInstance();
				this._pacienteRO.saveOrUpdate(this._paciente, catchOk);
			//}
		}

		public function catchOk(event:ResultEvent):void{
			retorno = event.result as Boolean;
			if (retorno) {
				Alert.show("Paciente gravado com sucesso");
				onClose(new CloseEvent("ok"));
			}
		}
		
		private function enterPress(event:KeyboardEvent):void {
			if (event.keyCode == Keyboard.ENTER) {
				this._view.focusManager.setFocus(this._view.focusManager.getNextFocusManagerComponent());				
			}
		}
		
		
	}
}